package com.driving.single.frontend.user.controller;

import com.driving.single.common.entity.DrivingResult;
import com.driving.single.common.utils.RegexUtils;
import com.driving.single.frontend.user.dto.param.AccountParam;
import com.driving.single.frontend.user.dto.param.PhoneLoginParam;
import com.driving.single.frontend.user.dto.param.PhoneRegisterParam;
import com.driving.single.frontend.user.dto.param.UsernameParam;
import com.driving.single.frontend.user.service.FrontendUserService;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 用户模块  控制层
 *
 * @author xsfno1
 * @version v1.0.0
 * @date 2020/2/20
 **/
@RestController
@RequestMapping(value = "/frontend/user", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class FrontendUserController {
    @Resource
    private FrontendUserService frontendUserService;

    /**
     * 用户登录(帐号密码)接口
     *
     * @param accountParam {@link AccountParam} 用户登录参数
     */
    @PostMapping("/login")
    public DrivingResult userLogin(@RequestBody AccountParam accountParam) {
        // 登录参数校验,错误报异常
        RegexUtils.verifyAccountLoginParam(accountParam);
        return this.frontendUserService.userLogin(accountParam.getAccount(), accountParam.getPassword());
    }

    /**
     * 用户登录 手机验证码发送 接口
     *
     * @param phone {@link String} 用户注册手机号码
     */
    @PostMapping("/sendLoginSms/{phone}")
    public DrivingResult sendLoginSms(@PathVariable String phone) {
        // 手机号正则验证,错误报异常
        RegexUtils.verifyPhone(phone);
        return this.frontendUserService.sendLoginSms(phone);
    }

    /**
     * 用户登录(手机验证码)接口
     *
     * @param phoneLoginParam {@link PhoneLoginParam} 用户[手机短信]登录参数类
     */
    @PostMapping("/loginWithPhoneCode")
    public DrivingResult userLoginWithPhoneCode(@RequestBody PhoneLoginParam phoneLoginParam) {
        // 手机号正则验证,错误报异常
        RegexUtils.verifyPhone(phoneLoginParam.getPhone());
        return this.frontendUserService.userLoginWithPhoneCode(phoneLoginParam.getPhone(),
                phoneLoginParam.getCode());
    }

    /**
     * 用户注册 手机验证码发送 接口
     *
     * @param phone 用户注册手机号码
     */
    @PostMapping("/sendRegisterSms/{phone}")
    public DrivingResult sendRegisterSms(@PathVariable String phone) {
        // 手机号正则验证,错误报异常
        RegexUtils.verifyPhone(phone);
        return this.frontendUserService.sendRegisterSms(phone);
    }

    /**
     * 用户注册(手机验证码) 接口
     *
     * @param phoneRegisterParam {@link PhoneRegisterParam} 用户[手机短信]注册参数类
     */
    @PostMapping("/registerWithPhoneCode")
    public DrivingResult userRegisterWithCode(@RequestBody PhoneRegisterParam phoneRegisterParam) {
        // 注册参数正则验证,错误报异常
        RegexUtils.verifyPhoneRegisterParam(phoneRegisterParam);
        return this.frontendUserService.userRegisterWithCode(phoneRegisterParam);
    }

    /**
     * 检查手机号是否重复 接口
     *
     * @param phone 手机号码
     */
    @PostMapping("/checkPhone/{phone}")
    public DrivingResult checkPhone(@PathVariable String phone) {
        // 手机号正则验证,错误报异常
        RegexUtils.verifyPhone(phone);
        return this.frontendUserService.checkPhone(phone);
    }

    /**
     * 检查用户名是否重复 接口
     *
     * @param usernameParam {@link UsernameParam} 用户名参数类
     */
    @PostMapping("/checkUsername")
    public DrivingResult checkUsername(@RequestBody UsernameParam usernameParam) {
        // 用户名正则验证,错误报异常
        RegexUtils.verifyUsername(usernameParam.getUsername());
        return this.frontendUserService.checkUsername(usernameParam.getUsername());
    }

    /**
     * 根据 用户ID 获取用户信息 接口
     *
     * @param uid 用户ID
     */
    @GetMapping("/sso/selectTbUserByUid")
    public DrivingResult selectTbUserById(Long uid) {
        return this.frontendUserService.selectTbUserById(uid);
    }


    /**
     * 用户评论 接口
     *
     * @param uid        用户ID
     * @param targetType 目标(驾校/教练/陪练)类型,1:驾校，2:教练，3:陪练
     * @param targetId   目标(驾校/教练/陪练)ID
     @RequestMapping("/sso/userComment") public DrivingResult userComment(Long uid, String targetType, Long targetId) {
     try {
     return this.frontendUserService.userComment(uid);
     } catch (Exception e) {
     e.printStackTrace();
     }
     return DrivingResult.error("error");
     }*/


}
